package health_system.health_system.chat;

import health_system.health_system.websocket.Model.ChatMessageHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

/**
 * WebSocket配置类
 * 配置聊天消息服务器的端点和处理器
 * 整合了之前分散在不同包的WebSocket配置
 */
@Configuration
@EnableWebSocket
public class ChatMessageServerConfig implements WebSocketConfigurer {

    /**
     * 注册WebSocket处理器和端点
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(chatMessageHandler(), "/chat")
                .setAllowedOrigins("*"); // 在生产环境中应限制允许的源
    }

    /**
     * 创建聊天消息处理器
     */
    @Bean
    public ChatMessageHandler chatMessageHandler() {
        return new ChatMessageHandler();
    }

    /**
     * 创建ServerEndpoint导出器，用于扫描和注册带有@ServerEndpoint注解的WebSocket端点
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
    
    /**
     * 配置WebSocket容器，设置消息缓冲区大小和超时时间
     */
    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        // Set the maximum text message buffer size (50MB)
        container.setMaxTextMessageBufferSize(50 * 1024 * 1024);
        // Set the maximum binary message buffer size (50MB)
        container.setMaxBinaryMessageBufferSize(50 * 1024 * 1024);
        // Set maximum session idle timeout (50 minutes)
        container.setMaxSessionIdleTimeout(50 * 60 * 1000L);
        // Enable asynchronous message processing
        container.setAsyncSendTimeout(5000L);
        
        return container;
    }
    
    /**
     * 配置WebSocket会话最大消息大小
     * 注：实际大小限制应根据系统需求调整
     */
    @Bean
    public ChatMessageServerProperties chatMessageServerProperties() {
        ChatMessageServerProperties properties = new ChatMessageServerProperties();
        properties.setMaxTextMessageSize(50 * 1024 * 1024); // 50MB
        properties.setMaxBinaryMessageSize(50 * 1024 * 1024); // 50MB
        properties.setAsyncSendTimeout(60000); // 60秒
        return properties;
    }
    
    /**
     * 聊天消息服务器属性类
     * 用于配置WebSocket的各种参数
     */
    public static class ChatMessageServerProperties {
        private int maxTextMessageSize;
        private int maxBinaryMessageSize;
        private long asyncSendTimeout;
        
        public int getMaxTextMessageSize() {
            return maxTextMessageSize;
        }
        
        public void setMaxTextMessageSize(int maxTextMessageSize) {
            this.maxTextMessageSize = maxTextMessageSize;
        }
        
        public int getMaxBinaryMessageSize() {
            return maxBinaryMessageSize;
        }
        
        public void setMaxBinaryMessageSize(int maxBinaryMessageSize) {
            this.maxBinaryMessageSize = maxBinaryMessageSize;
        }
        
        public long getAsyncSendTimeout() {
            return asyncSendTimeout;
        }
        
        public void setAsyncSendTimeout(long asyncSendTimeout) {
            this.asyncSendTimeout = asyncSendTimeout;
        }
    }
} 